package com.example.deviceinfo;

/**
 * Created by sunny on 15-8-11.
 */

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.storage.StorageManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class PrintDisk extends Activity {
    TextView device, device1;

    String info = "";

    public Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    device.setText(info);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.storage);
        device = (TextView) findViewById(R.id.device);
        device1 = (TextView) findViewById(R.id.device1);
        Storage.setContext(getApplicationContext());
//		getDeviceInfo(this);
//		printSdCard();
        listAvaliableStorage(this);
    }

    public String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            String device_id = tm.getDeviceId();

            android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);

            String mac = wifi.getConnectionInfo().getMacAddress();
            json.put("mac", mac);

            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }

            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
            }

            json.put("device_id", device_id);
//	      info = json.toString();
            info = "mac:  " + mac + "\ndevice_id:  " + device_id;
            Log.i("", "sunny :" + json.toString() + ";mac:" + mac + ";device_id:" + device_id);
            handler.sendEmptyMessage(0);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
            info = e.toString();
            Log.i("", "sunny exception:" + e.toString());
            handler.sendEmptyMessage(0);
        }
        Log.i("", "sunny : null");
        return null;
    }

    private void printSdCard() {
        String path;
        List<Storage.SDevice> devices = Storage.getSDeviceList();
        int count = devices.size();
        Log.i("", "sunny path 444:" + count);
        for (int i = 0; i < count; i++) {
            Storage.SDevice sdv = devices.get(i);
            if (sdv.getMediaType() == Storage.SDevice.MEDIA_TYPE.SDCARD) {
                path = sdv.getMountPoint();
                Log.i("", "sunny path 333:" + path);
                device.setText(device.getText() + "\nSdcard:" + path);
            } else if (sdv.getMediaType() == Storage.SDevice.MEDIA_TYPE.DISK) {
                path = sdv.getMountPoint();
                Log.i("", "sunny path 444:" + path);
                device1.setText(device1.getText() + "\nDisk:" + path);
            }
        }
    }

    public List<StorageInfo> listAvaliableStorage(Context context) {
        ArrayList<StorageInfo> storagges = new ArrayList<StorageInfo>();
        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        try {
            Class<?>[] paramClasses = {};
            Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
            getVolumeList.setAccessible(true);
            Object[] params = {};
            Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
            if (invokes != null) {
                StorageInfo info = null;
                for (int i = 0; i < invokes.length; i++) {
                    Object obj = invokes[i];
                    Method getPath = obj.getClass().getMethod("getPath", new Class[0]);
                    String path = (String) getPath.invoke(obj, new Object[0]);
                    info = new StorageInfo(path);
                    File file = new File(info.path);
                    if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) {
                        Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
                        Method isEmulate = obj.getClass().getMethod("isEmulated", new Class[0]);
                        String state = null;
                        try {
                            Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
                            state = (String) getVolumeState.invoke(storageManager, info.path);
                            info.state = state;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        if (info.isMounted()) {
                            info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();
                            info.isEmulate = ((Boolean) isEmulate.invoke(obj, new Object[0])).booleanValue();
                            print(info);
                            storagges.add(info);
                        }
                    }
                }
            }
        } catch (NoSuchMethodException e1) {
            e1.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        storagges.trimToSize();

        return storagges;
    }

    void print(StorageInfo info) {
        String str = "\nprint path:" + info.path + "; state:" + info.state + "; isRemoveable:"
                + info.isRemoveable + "; isEmulate；" + info.isEmulate;
        Log.i("", str);
        device1.setText(device1.getText() + str);
    }

    public class StorageInfo {
        public String path;
        public String state;
        public boolean isRemoveable;
        public boolean isEmulate;

        public StorageInfo(String path) {
            this.path = path;
        }

        public boolean isMounted() {
            return "mounted".equals(state);
        }
    }
}
